<template>
  <comcard>
    <div slot="com_title">
      <el-row>
        <el-col :sm="13">
          <el-form ref="form" :model="form" label-width="100px">
            <el-form-item label="店铺名称">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
            <el-form-item label="店铺公告">
              <el-input type="textarea" v-model="form.bulletin"></el-input>
            </el-form-item>
            <el-form-item label="店铺头像">
              <el-upload
                class="avatar-uploader"
                action="http://localhost:5000/shop/upload"
                :show-file-list="false"
                :on-success="handleAvatarSuccess"
                :before-upload="beforeAvatarUpload"
              >
                <img v-if="imageUrl" :src="imageUrl" class="avatar" />
                <i v-else class="el-icon-plus avatar-uploader-icon"></i>
              </el-upload>
            </el-form-item>
            <el-form-item label="店铺图片">
              <el-upload
                action="http://localhost:5000/shop/upload"
                list-type="picture-card"
                :on-preview="yulanImage"
                :on-remove="handleRemove"
                :file-list="fileList"
                :on-success="headerList"
                :before-upload="beforeAvatarUpload"
              >
                <i class="el-icon-plus"></i>
              </el-upload>
              <el-dialog :visible.sync="dialogVisible">
                <img width="100%" :src="dialogImageUrl" alt="" />
              </el-dialog>
            </el-form-item>
            <el-form-item label="配送费">
              <el-input v-model="form.deliveryPrice"></el-input>
            </el-form-item>
            <el-form-item label="配送时间">
              <el-input v-model="form.deliveryTime"></el-input>
            </el-form-item>
            <el-form-item label="配送描述">
              <el-input v-model="form.description"></el-input>
            </el-form-item>
            <el-form-item label="店铺评分">
              <el-input v-model="form.score"></el-input>
            </el-form-item>
            <el-form-item label="销量">
              <el-input v-model="form.sellCount"></el-input>
            </el-form-item>
            <el-form-item label="活动：">
              <el-checkbox-group v-model="cks">
                <el-checkbox label="单人精彩套餐" border></el-checkbox>
                <el-checkbox label="VC无限橙果汁全场8折" border></el-checkbox>
                <el-checkbox label="复选框 C" border></el-checkbox>
              </el-checkbox-group>
            </el-form-item>
            <el-form-item label="营业时间">
              <el-date-picker
                v-model="startdate"
                type="datetimerange"
                range-separator="至"
                start-placeholder="开始日期"
                end-placeholder="结束日期"
              >
              </el-date-picker>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="onSubmit">立即创建</el-button>
              <el-button>取消</el-button>
            </el-form-item>
          </el-form>
        </el-col>
      </el-row>
    </div>
    <div slot="com_content"></div>
  </comcard>
</template>

<script>
import { api_shopinfo,api_shopUpdate } from "../../api/shop";
import Comcard from "../../components/comcard";
export default {
  components: { Comcard },
  data() {
    return {
      form: {
        name: "",
        bulletin: "",
        avatar: "",
        deliveryPrice: "",
        deliveryTime: "",
        description: "",
        score: "",
        sellCount: "",
        supports: [],
        pics: "",
        date: "",
        minPrice: "",
        startdate:[],
     
      },
      imageUrl: "",
      dialogImageUrl: "",
      dialogVisible: false,
      fileList:[],
         cks:[],
    };
  },
  mounted() {
    this.query();
  },

  methods: {
    headerList(res,file){
      this.form.pics.push(res.imgUrl);
      console.log(res);
      this.fileList.push({
        name:res.imgUrl,
        url:`http://localhost:5000/upload/shop/${res.imgUrl}`
      })
    },
    handleRemove(file, fileList) {
      this.form.pics=fileList.map((item)=>{
        return item.name;
      })
      this.fileList=fileList;
      console.log(file, fileList);
    },
    yulanImage(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    async query() {
      let rst = await api_shopinfo(this.form);
      console.log(rst);
      this.imageUrl = "http://localhost:5000/upload/shop/" + rst.data.avatar;
      // 数据渲染,之后回填
      this.form = rst.data;
      this.startdate=rst.data.date;
      this.cks=rst.data.supports;
      // 图片显示值回填
      this.fileList=this.form.pics.map(item=>{
        return {name:item,url:`http://localhost:5000/upload/shop/${item}`};
      })
    },
    async onSubmit() {
      this.form.date=JSON.stringify(this.startdate);
                this.form.pics=JSON.stringify(this.form.pics);
                this.form.supports=JSON.stringify(this.cks);
               let rst= await api_shopUpdate(this.form);
                let {code,msg}=rst;
                if(code==0){
                    this.$message.success(msg);
                }
    },
    handleAvatarSuccess(res, file) {
      console.log(res);
      // 这是传给数据库的，数据库只要图片名称
      this.form.avatar = res.imgUrl;
      this.imageUrl = "http://localhost:5000/upload/shop/" + res.imgUrl;
    },
    beforeAvatarUpload(file) {
      const isJPG = file.type === "image/jpeg" || file.type === "image/png";
      const isLt2M = file.size / 1024 / 1024 < 2;

      if (!isJPG) {
        this.$message.error("上传头像图片只能是 JPG 格式!");
      }
      if (!isLt2M) {
        this.$message.error("上传头像图片大小不能超过 2MB!");
      }
      return isJPG && isLt2M;
    },
  },
};
</script>

<style lang="less" scoped>
/deep/.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}
.avatar-uploader .el-upload:hover {
  border-color: #409eff;
}
.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 150px;
  height: 150px;
  line-height: 150px;
  text-align: center;
}
.avatar {
  width: 150px;
  height: 150px;
  display: block;
}
</style>
